package main

import (
	"fmt"
)

type ListNode struct {
	Val  int
	Next *ListNode
}

func main() {

	l6 := &ListNode{
		Val:  7,
		Next: nil,
	}
	l5 := &ListNode{
		Val:  6,
		Next: l6,
	}
	l4 := &ListNode{
		Val:  3,
		Next: l5,
	}
	l3 := &ListNode{
		Val:  3,
		Next: l4,
	}
	l2 := &ListNode{
		Val:  3,
		Next: l3,
	}
	l := &ListNode{
		Val:  1,
		Next: l2,
	}
	lists := deleteDuplicates(l)
	fmt.Println("l1 = ", lists.Val)
	fmt.Println("l2 = ", lists.Next.Val)
	fmt.Println("l3 = ", lists.Next.Next.Val)
}

/*
func deleteDuplicates(head *ListNode) *ListNode {
	if head == nil || head.Next == nil {
		return nil
	}
	cur := head
	for cur != nil && cur.Next != nil {
		if cur.Val == cur.Next.Val {
			cur.Next = cur.Next.Next
		} else {
			cur = cur.Next
		}
	}
	return head
}
*/

/*
func deleteDuplicates(head *ListNode) *ListNode {
	if head == nil || head.Next == nil {
		return head
	}
	if head.Val == head.Next.Val {
		return deleteDuplicates(head.Next)
	} else {
		head.Next = deleteDuplicates(head.Next)
	}
	return head
}
*/

func deleteDuplicates(head *ListNode) *ListNode {
	if head == nil {
		return nil
	}
	dummy := &ListNode{0, head}
	cur := dummy
	for cur.Next != nil && cur.Next.Next != nil {
		if cur.Next.Val == cur.Next.Next.Val {
			x := cur.Next.Val
			for cur.Next != nil && cur.Next.Val == x {
				cur.Next = cur.Next.Next
			}
		} else {
			cur = cur.Next
		}
	}
	return dummy.Next
}
